"""Selectivity Index Model

.. helpdoc::
Calculates the Selectivity index from a dose response model.
"""


"""<widgetXML>
    <name>Selectivity Index Model<!-- [REQUIRED] title --></name>
    <icon>default.png</icon>
    <tags>
        <tag>Dose Response</tag>
    </tags>
    <summary>Calculates the Selectivity index from a dose response model.</summary>
    <author>
        <authorname>Red-R Core Development Team</authorname>
        <authorcontact>www.red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

"""
<name>Selectivity Index</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description>Compares selectivity indices for arbitrary dosage across curves. The selectivity is the ratio between effective dosages from different curves.</description>
<RFunctions>drc:SI</RFunctions>
<tags>Dose Response</tags>
<icon>drc.PNG</icon>
"""
from OWRpy import * 
import signals, redRGUI

class RedRSI(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, wantGUIDialog = 1, **kwargs)
        self.setRvariableNames(["SI"])
        self.data = {}
        self.require_librarys(["drc"])
        self.RFunctionParam_object = ''
        
        
        """.. rrsignals::"""
        self.inputs.addInput("object", "object", signals.base.RList, self.processobject)
        
        """.. rrsignals::"""
        self.outputs.addOutput("SI Output","SI Output", signals.base.RMatrix)
        
        box = redRGUI.base.gridBox(self.controlArea)
        
        """.. rrgui::"""
        self.RFunctionParamlogBase_lineEdit = redRGUI.base.lineEdit(self.GUIDialog, label = "Base for Log Transformation:", text = 'NULL')
        """.. rrgui::"""
        self.RFunctionParamod_radioButtons = redRGUI.base.radioButtons(self.GUIDialog, label = "Over Dispersion Compensation:", buttons = ["TRUE", "FALSE"], setChecked = "FALSE", orientation = 'horizontal')
        
        """.. rrgui::"""
        self.RFunctionParamreverse_radioButtons = redRGUI.base.radioButtons(self.GUIDialog, label = "Reverse:", buttons = ["TRUE", "FALSE"], setChecked = "", orientation = 'horizontal')
        
        """.. rrgui::"""
        self.RFunctionParamreference_radioButtons = redRGUI.base.radioButtons(self.GUIDialog, label = "Reference:", buttons = ["control","upper"], setChecked = "upper", orientation = 'horizontal')
        
        """.. rrgui::"""
        self.RFunctionParamlevel_lineEdit = redRGUI.base.lineEdit(box.cell(0,0), label = "Confidence Level:", text = '0.95')
        
        """.. rrgui::"""
        self.RFunctionParaminterval_comboBox = redRGUI.base.comboBox(box.cell(0,1), label = "Confidence Interval Type:", items = ["none","delta","fieller","fls"])
        
        """.. rrgui::"""
        self.RFunctionParamtype_radioButtons = redRGUI.base.radioButtons(self.GUIDialog, label = "Response Levels:", buttons = ["relative","absolute"], setChecked = "relative", orientation = 'horizontal')
        
        """.. rrgui::"""
        self.RFunctionParampool_radioButtons = redRGUI.base.radioButtons(self.GUIDialog, label = "Pool Curves:", buttons = ["TRUE", "FALSE"], setChecked = "TRUE", orientation = 'horizontal')
        
        """.. rrgui::
            :description: `Input a list of doses to be compared, this is required.  You may need to know the range in which your experiment was run but an example might be "10, 20, 50" for doses.\nNo units are used.  The doss are the doses that will be used for each drug, if more doses are supplied than curves, then levels are given for all combinations.\nYou might need to experiment some to get what you are looking for.`
        """
        self.RFunctionParampercVec_lineEdit = redRGUI.base.lineEdit(box.cell(1,0), label = "Contrasts (required):", text = '', toolTip = 'Input a list of doses to be compared, this is required.  You may need to know the range in which your experiment was run but an example might be "10, 20, 50" for doses.\nNo units are used.  The doss are the doses that will be used for each drug, if more doses are supplied than curves, then levels are given for all combinations.\nYou might need to experiment some to get what you are looking for.')
        
        """.. rrgui::"""
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
        
        """.. rrgui::"""
        self.RoutputWindow = redRGUI.base.textEdit(self.controlArea, label = "R Output Window")
    
    def processobject(self, data):
        if data:
            self.RFunctionParam_object=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_object=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_object) == '': return
        if unicode(self.RFunctionParampercVec_lineEdit.text()) == '':
            self.status.setText('No contrasts set')
            return
        injection = []
        if unicode(self.RFunctionParamlogBase_lineEdit.text()) != '':
            string = ',logBase='+unicode(self.RFunctionParamlogBase_lineEdit.text())+''
            injection.append(string)
        ## make commit function for self.RFunctionParamod_radioButtons
        injection.append(',od = %s' % self.RFunctionParamod_radioButtons.getChecked())
        ## make commit function for self.RFunctionParamreverse_radioButtons
        injection.append(',reverse = %s' % self.RFunctionParamreverse_radioButtons.getChecked())
        ## make commit function for self.RFunctionParamreference_radioButtons
        injection.append(',reference = "%s"' % self.RFunctionParamreference_radioButtons.getChecked())
        if unicode(self.RFunctionParamlevel_lineEdit.text()) != '':
            string = ',level='+unicode(self.RFunctionParamlevel_lineEdit.text())+''
            injection.append(string)
        string = ',interval=\''+unicode(self.RFunctionParaminterval_comboBox.currentText())+'\''
        injection.append(string)
        ## make commit function for self.RFunctionParamtype_radioButtons
        injection.append(',type="%s"' % self.RFunctionParamtype_radioButtons.getChecked())
        #make commit function for self.RFunctionParampool_radioButtons
        injection.append(',pool=%s' % self.RFunctionParampool_radioButtons.getChecked())
        if unicode(self.RFunctionParampercVec_lineEdit.text()) != '':
            string = ',percVec=c('+unicode(self.RFunctionParampercVec_lineEdit.text())+')'
            injection.append(string)
        inj = ''.join(injection)
        self.R(self.Rvariables['SI']+'<-SI(object='+unicode(self.RFunctionParam_object)+','+inj+')')
        self.R('txt<-capture.output('+self.Rvariables['SI']+')')
        self.RoutputWindow.clear()
        tmp = self.R('paste(txt, collapse ="\n")')
        self.RoutputWindow.insertPlainText(''+tmp+'')
        
        newData = signals.RMatrix.RMatrix(self, data = self.Rvariables["SI"])
        self.rSend("SI Output", newData)